import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/5/21 10:17
 */
public class Join_2 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        // 状态在内存中的保存时间
//        tEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(10));
        
        tEnv.executeSql("create table t1(" +
                            "id string, " +
                            "name string, " +
                            "ts bigint, " +
                            "et as to_timestamp_ltz(ts, 3), " +
                            "watermark for et as et" +
                            ")with(" +
                            " 'connector' = 'kafka', " +
                            " 'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            " 'properties.group.id' = 'atguigu', " +
                            " 'topic' = 't1', " +
                            " 'format' = 'json', " +
                            " 'scan.startup.mode' = 'latest-offset'" +
                            ")");
    
        tEnv.executeSql("create table t2(" +
                            "id string, " +
                            "age int, " +
                            "et timestamp(3) METADATA FROM 'values.source.timestamp' VIRTUAL," +
                            "watermark for et as et, " +
                            "primary key(id) not enforced " +
                            ")with(" +
                            " 'connector' = 'kafka', " +
                            " 'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            " 'properties.group.id' = 'atguigu', " +
                            " 'topic' = 't2', " +
                            " 'format' = 'json', " +
                            " 'scan.startup.mode' = 'latest-offset'" +
                            ")");
        
        
        tEnv.sqlQuery("select " +
                          "t1.id, t1.name, t1.et, " +
                          "t2.id, t2.age, t2.et " +
                          "from t1 " +
                          "join t2 for system_time as of t1.et " +
                          "on t1.id=t2.id")
            .execute()
            .print();
        
        
        
        
       
    }
}
